package com.ohuang.test_compose.page

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.StableMarker
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.ohuang.test_compose.ui.theme.CustomTheme
import com.ohuang.test_compose.ui.theme.Custom_Theme
import com.ohuang.test_compose.ui.theme.Test_composeTheme
import kotlin.random.Random

@Composable
fun StatePage() {
    Test_composeTheme() {

        Surface(modifier = Modifier.background(CustomTheme.colors.background)) {
            Column() {
                val state = remember {
                    mutableStateOf("123")
                }

                val mutableString = remember {
                    MutableString("123")
                }
                val mutableStringImmutable = remember {
                    MutableStringImmutable("123")
                }
                val stableMutableString = remember {
                    StableMutableString("123")
                }
                val mutableString3 = remember {
                    MutableString3(MutableString("123"))
                }
                val mutableString2 = remember {
                    MutableString2(StableMutableString("123"))
                }

                val immutableString = remember {
                    ImmutableString("123")
                }

                Button(onClick = {
                    state.value = "state2:" + System.currentTimeMillis()
                }) {
                    Text(text = "点击重组:"+state.value, color = CustomTheme.colors.textPrimary)
                }
                Text(text = state.value)
                showMutableString(mutableString)
                showMutableStringImmutable(mutableStringImmutable)
                showImmutableString(immutableString)
                showStableMutableString(stableMutableString)
                showMutableString2(mutableString2)
                showMutableString3(mutableString3)
                Text(text = "=======================以下每次传入的都是新实例")
                StringStateTest(StringState("123"))
                StringImmutableTest(StringImmutable("123"))
                StringNoramlTest(StringNoraml("123"))
                Text(text = "===========以下实例相同,但值已更改")
                var stringState = remember {
                    StringState("123")
                }

                var  stringImmutable= remember {
                    StringImmutable("123")
                }
                var  stringNoraml= remember {
                    StringNoraml("123")
                }

                stringState.string=state.value
//                stringStableMarker.string=state.value
                stringImmutable.string=state.value
                stringNoraml.string=state.value
                StringStateTest(stringState)

                StringImmutableTest(stringImmutable)
                StringNoramlTest(stringNoraml)
            
            }

        }

    }
}





@Stable
data class StringState(var string: String){

}
@Composable
fun StringStateTest(s:StringState){
    Text(text ="StringStateTest="+ s, color = getRandomColor())
}


@Immutable
data class StringImmutable(var string: String)

@Composable
fun StringImmutableTest(s:StringImmutable){
    Text(text ="StringImmutableTest="+ s.string, color = getRandomColor())
}
data class StringNoraml(var string: String)

@Composable
fun StringNoramlTest(s:StringNoraml){
    Text(text = "StringNoraml="+s.string, color = getRandomColor())
}


//1. 不可变类型：String
@Composable
fun showString(string: String) {
    Text(text = "showString ${string}", color = getRandomColor())
}

//2. 可变类型：有可变的属性
class MutableString(var data: String)

@Immutable
class MutableStringImmutable(var data: String)

@Composable
fun showMutableString(string: MutableString) {
    Text(text = "存在var参数。非稳态 ${string.data}", color = getRandomColor())
}

fun getRandomColor() = Color(
    red = Random.nextInt(256),
    green = Random.nextInt(256),
    blue = Random.nextInt(256),
    alpha = 255
)

@Composable
fun showMutableStringImmutable(string: MutableStringImmutable) {
    Text(text = "非稳态数据加@Immutable注解 。稳态${string.data}", color = getRandomColor())
}

//3. 不可变类型：成员属性全是 final
class ImmutableString(val data: String)

@Composable
fun showImmutableString(string: ImmutableString) {
    Text(text = "所以参数都是val。稳态 ${string.data}", color = getRandomColor())
}

//4. 可变类型加 @Stable 注解
@Stable
class StableMutableString(var data: String)

@Composable
fun showStableMutableString(string: StableMutableString) {
    Text(text = "非稳态数据加@State注解。稳态 ${string.data}", color = getRandomColor())
}

//5. 变化可被追踪
class MutableString2(
    val data: StableMutableString = StableMutableString("")
)

@Composable
fun showMutableString2(string: MutableString2) { //可被追踪  MutableString2如果没有变化会跳过重组
    Text(text = "参数为val类型,但是参数为稳态。稳态 ${string.data.data}", color = getRandomColor())
}

class MutableString3(
    val data: MutableString,
)

@Composable
fun showMutableString3(string: MutableString3) { //MutableString3不可追踪 MutableString3没有变化也会执行
    Text(text = "参数为val类型,但是参数为非稳态。 非稳态数据 ${string.data.data}", color = getRandomColor())
}